<?php
/**
 * Created by PhpStorm.
 * User: Frings
 * Date: 2016/9/9
 * Time: 9:29
 */

namespace app\models;


use app\librarys\Tools;

Class CardSale
{
    /**
     * 获取销售名片数量
     * @param $page
     * @param $pagesize
     * @param $name
     * @param $saler
     * @param $voucher
     * @param $begin_create_datetime
     * @param $end_create_datetime
     * @return false|null|string
     */
    public static function getCardSaleCount($name, $saler, $voucher, $begin_create_datetime, $end_create_datetime)
    {
        $sql = 'SELECT count(*) FROM t_card_sale INNER JOIN t_manager ON t_manager.id = t_card_sale.create_manager_id WHERE 1 = 1 ';
        if ($name != '') {
            $sql = $sql . ' AND t_card_sale.name Like :name';
        }
        if ($saler != '') {
            $sql = $sql . ' AND saler Like :saler';
        }
        if ($voucher != '') {
            $sql = $sql . ' AND start_index <= :voucher AND end_index >= :voucher';
        }
        if ($begin_create_datetime != '') {
            $sql = $sql . ' AND create_datetime >= :begin_create_datetime';
        }
        if ($end_create_datetime != '') {
            $sql = $sql . ' AND create_datetime <= :end_create_datetime';
        }

        $cmd = \Yii::$app->db->createCommand($sql);
        if ($name != '') {
            $cmd->bindValue(':name', '%' . $name . '%');
        }
        if ($saler != '') {
            $cmd->bindValue(':saler', '%' . $saler . '%');
        }
        if ($voucher != '') {
            $cmd->bindValue(':voucher', $voucher);
        }
        if ($begin_create_datetime != '') {
            $cmd->bindValue(':begin_create_datetime', $begin_create_datetime);
        }
        if ($end_create_datetime != '') {
            $cmd->bindValue(':end_create_datetime', $end_create_datetime);
        }

        $result = $cmd->queryScalar();
        return $result;
    }

    /**
     * 获取销售名片列表
     * @param $page
     * @param $pagesize
     * @param $name
     * @param $saler
     * @param $voucher
     * @param $begin_create_datetime
     * @param $end_create_datetime
     * @return array
     */
    public static function getCardSaleList($page, $pagesize, $name, $saler, $voucher, $begin_create_datetime, $end_create_datetime)
    {
        $sql = 'SELECT t_card_sale.*,t_manager.name AS create_manager_name FROM t_card_sale INNER JOIN t_manager ON t_manager.id = t_card_sale.create_manager_id WHERE 1 = 1 ';
        if ($name != '') {
            $sql = $sql . ' AND t_card_sale.name Like :name';
        }
        if ($saler != '') {
            $sql = $sql . ' AND saler Like :saler';
        }
        if ($voucher != '') {
            $sql = $sql . ' AND start_index <= :voucher AND end_index >= :voucher';
        }
        if ($begin_create_datetime != '') {
            $sql = $sql . ' AND create_datetime >= :begin_create_datetime';
        }
        if ($end_create_datetime != '') {
            $sql = $sql . ' AND create_datetime <= :end_create_datetime';
        }
        $sql = $sql . ' ORDER BY create_datetime DESC LIMIT :start,:pagesize';

        $cmd = \Yii::$app->db->createCommand($sql);
        if ($name != '') {
            $cmd->bindValue(':name', '%' . $name . '%');
        }
        if ($saler != '') {
            $cmd->bindValue(':saler', '%' . $saler . '%');
        }
        if ($voucher != '') {
            $cmd->bindValue(':voucher', $voucher);
        }
        if ($begin_create_datetime != '') {
            $cmd->bindValue(':begin_create_datetime', $begin_create_datetime);
        }
        if ($end_create_datetime != '') {
            $cmd->bindValue(':end_create_datetime', $end_create_datetime);
        }
        //计算起始数据行
        if ($page <= 0) $page = 0;
        $start = ($page - 1) * $pagesize;

        $cmd->bindValue(':start', $start);
        $cmd->bindValue(':pagesize', $pagesize);

        $result = $cmd->queryAll();
        return $result;
    }

    /**
     * 删除销售名片
     * @param $id
     * @return bool
     */
    public static function delCardSale($id)
    {
        $sql = 'DELETE FROM t_card_sale WHERE id = :id';
        $cmd = \Yii::$app->db->createCommand($sql);
        $cmd->bindValue(':id', $id);

        $result = $cmd->execute();
        return $result > 0;
    }

    /**
     * 保存销售名片
     * @param $saler
     * @param $name
     * @param $description
     * @param $start_index
     * @param $end_index
     * @param $creat_manager_id
     * @return bool
     */
    public static function saveCardSale($saler, $name, $description, $start_index, $end_index, $creat_manager_id)
    {
        $sql = 'INSERT INTO t_card_sale(id,saler,name,description,start_index,end_index,create_manager_id,create_datetime)
                VALUES(uuid(),:saler,:name,:description,:start_index,:end_index,:create_manager_id,now())';
        $cmd = \Yii::$app->db->createCommand($sql);
        $cmd->bindValue(':saler', $saler);
        $cmd->bindValue(':name', $name);
        $cmd->bindValue(':description', $description);
        $cmd->bindValue(':start_index', $start_index);
        $cmd->bindValue(':end_index', $end_index);
        $cmd->bindValue(':create_manager_id', $creat_manager_id);

        $result = $cmd->execute();
        return $result > 0;
    }

}